﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.LongRunningTaskManager.Interface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.LongRunningTaskManager.Tests
{
    [TestClass]
    public class LongRunningTaskFactoryTests
    {
        private static Dictionary<LongRunningTaskTypeEnum, Type> _taskTypes;
        private ILongRunningTaskFactory _taskFactory;

        [ClassInitialize]
        public static void ClassInitialize(TestContext testContext)
        {
            _taskTypes = new Dictionary<LongRunningTaskTypeEnum, Type>
            {
                { new MyTaskInt().Type, typeof(MyTaskInt) },
                { new MyTaskString().Type, typeof(MyTaskString) },
                { new MyTaskObject().Type, typeof(MyTaskObject) },
                { new MyTaskFileInMemEnforce().Type, typeof(MyTaskFileInMemEnforce) },
                { new MyTaskFileInMem().Type, typeof(MyTaskFileInMem) },
                { new MyTaskFileOnDisk().Type, typeof(MyTaskFileOnDisk) }
            };
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _taskFactory = new LongRunningTaskFactory();
        }


        [TestMethod]
        public void GetLongRunningTaskTypes_Success()
        {
            var types = _taskFactory.GetLongRunningTaskTypes();

            Assert.IsNotNull(types, "Returned value is null.");
            Assert.IsTrue(_taskTypes.Select(el => el.Value).All(t => types.Any(ft => ft == t)), "Not all task types were returned.");
        }

        [TestMethod]
        [ExpectedException(typeof(LongRunningTaskTypeNotSupportedException))]
        public void GetLongRunningTaskType_NotSupportedException()
        {
            _taskFactory.GetLongRunningTaskType((LongRunningTaskTypeEnum)9999999);
        }

        [TestMethod]
        public void GetLongRunningTaskType_Success()
        {
            var type = _taskFactory.GetLongRunningTaskType(new MyTaskString().Type);

            Assert.IsNotNull(type, "Returned value is null.");
            Assert.IsTrue(type == typeof(MyTaskString), "Returned value is different than the expected value.");
        }

        [TestMethod]
        [ExpectedException(typeof(LongRunningTaskTypeNotSupportedException))]
        public void CreateLongRunningTask_NotSupportedException()
        {
            _taskFactory.CreateLongRunningTask((LongRunningTaskTypeEnum)9999999);
        }

        [TestMethod]
        public void CreateLongRunningTask_Success()
        {
            var task = _taskFactory.CreateLongRunningTask(new MyTaskString().Type);

            Assert.IsNotNull(task, "Returned value is null.");
            Assert.IsTrue(task.GetType() == typeof(MyTaskString), "Returned value has different type than expected.");
        }
    }
}
